//
//  StatusViewModel.swift
//  ZHTQWeibo
//
//  Created by ZHTQ on 2018/9/20.
//  Copyright © 2018年 ZHTQ. All rights reserved.
//

import UIKit

class StatusViewModel: NSObject {
    /// 微博数据
    var status: Status?
    
    init(status: Status){
        self.status = status
        // 头像地址
        if let image = status.user?.profile_image_url {
            iconURL = URL(string: image)
        }
        
        // 来源
        if let from : NSString = status.source! as NSString? , from != ""{
            let lo = from.range(of: ">").location + 1
            let le = from.range(of: "<", options: NSString.CompareOptions.backwards).location - lo
            source_text = "来自" + from.substring(with: NSRange.init(location: lo, length: le))
        }
        
        // 时间 Thu Sep 20 20:04:54 +0800 2018
        if let timeStr = status.created_at {
            // 获取当前时间
            let createdDate = Date.createdDate(timeStr: timeStr, formatStr: "EEE MM dd HH:mm:ss Z yyyy")
            // 设置字符串微博时间
            created_time = createdDate.descreptionStr()
        }
        
        // vip image
        if let vip = status.user?.mbrank {
            if 0 < vip && vip <= 6 {
                mbrank_image = UIImage(named: "common_icon_membership_level\(vip)")
            }
        }
        
        // 认证
        if let verfield = status.user?.verified_type {
            switch verfield{
            case 0:
                verified_image = #imageLiteral(resourceName: "avatar_vip")
            case 2, 3, 5:
                verified_image = #imageLiteral(resourceName: "avatar_enterprise_vip")
            case 220:
                verified_image = #imageLiteral(resourceName: "avatar_grassroot")
            default:
                verified_image = nil
            }
        }
        
        if let urls = status.retweeted_status != nil ? status.retweeted_status?.pic_urls : status.pic_urls {
            
            var thumb = [URL]()
            var thumbmiddle = [URL]()
            
            for url in urls {
                guard let urlStr = url["thumbnail_pic"] as? String else {
                    // 图片地址为空 掉过加载下一个地址
                    continue
                }
                thumb.append(URL(string: urlStr)!)
                
                let bmiddle = urlStr.replacingOccurrences(of: "thumbnail", with: "bmiddle")
                thumbmiddle.append(URL(string: bmiddle)!)
            }
            
            // 保存微博图片地址
            thumbnail_urls = thumb
            // 保存微博大图地址
            bmiddle_urls = thumbmiddle
        }
        
        if let text = status.retweeted_status?.text{
            let name = status.retweeted_status?.user?.screen_name ?? ""
            forwoardText = "@" + name + "：" + text
        }
        
    }
    
    /// 用户头像地址
    var iconURL: URL?
    
    /// 微博来源
    var source_text: String = ""
    
    /// 微博创建时间
    var created_time: String = ""
    
    /// 是否是微博认证用户图片
    var verified_image: UIImage?
    
    /// 微博会员等级
    var mbrank_image: UIImage?
    
    /// 微博内容图片地址
    var thumbnail_urls: [URL]?
    
    /// 微博内容大图片地址
    var bmiddle_urls: [URL]?
    
    /// 微博内容文字
    var forwoardText: String?
}
